<template>
	<view style="margin-bottom: 130rpx;">
		<view class="cu-form-group margin-top">
			<view class="title">名字</view>
			<input placeholder="请输入名字" v-model="contacts" type="text"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">电话</view>
			<input placeholder="请输入电话" v-model="phone" type="number" maxlength="11"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">位置</view>
			<input placeholder="请输入位置" v-model="place" type="text"></input>
			<!-- #ifdef MP-WEIXIN -->
			<text class='cuIcon-locationfill text-orange' @click="openMap"></text>
			<!-- #endif -->
		</view>

		<!-- 详情 -->
		<view class="cu-form-group margin-top">
			<view class="title">介绍</view>
		</view>
		<view class="cu-form-group">
			<textarea maxlength="200" :disabled="modalName!=null" v-model="textarea" placeholder="请对自我进行介绍"></textarea>
		</view>

		<!-- 选择上传图片或视频 -->
		<view class="cu-form-group margin-top">
			<view class="title">选择上传图片或视频</view>
			<switch @change="selectIm" :class="switchA?'checked':''" :checked="switchA?true:false"></switch>
		</view>
		<!-- 图片上传 -->
		<view class="" v-if="!switchA">
			<view class="cu-bar bg-white">
				<view class="action">
					图片上传
				</view>
				<view class="action">
					{{imgList.length}}/4
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage"
						:data-url="imgList[index]">
						<image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
		</view>
		<!-- 视频上传 -->
		<view class="" v-else>
			<view class="cu-bar bg-white">
				<view class="action">
					视频上传 暂时不支持
				</view>
				<view class="action">
					只能上传一个不超过60秒的视频
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="solids" @tap="ChooseVideo" v-if="video.length == 0">
						<text class='cuIcon-cameraadd'></text>
					</view>
					<view class="bg-img" v-else>
						<video :src="video" class="video"></video>
						<view class="cu-tag bg-red" @tap.stop="DelVideo" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 按钮 -->
		<view class="padding flex flex-direction">
			<button class="cu-btn bg-green margin-tb-sm lg" :disabled="forbid" form-type="submit"
				@click="submitForm">提交</button>
		</view>
		<view class="padding flex flex-direction">
			<button class="cu-btn bg-green margin-tb-sm lg" @click="skipQuery">查询列表</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 联系人
				contacts: null,
				// 联系电话
				phone: null,
				// 详细位置
				place: null,
				// 描述
				textarea: null,
				modalName: null,
				// 选择上传图片或视频
				switchA: false,
				// 图片数组
				imgList: [],
				imgDataList: [],
				// 视频
				video: '',
			}
		},
		computed: {
			forbid() {
				return this.contacts == null || this.phone == null || this.place == null || this
					.textarea == null || this.imgList == ''
			}
		},
		methods: {
			// 提交
			async submitForm() {
				let data = {
					contacts: this.contacts, //	（联系人）
					phone: this.phone, //（电话）
					address: this.place, //（地址）
					description: this.textarea, //（问题描述）
					img_arr: this.imgDataList
				}
				this.uploadingImg()
				console.log(data);
				let res = await this.$operate.uniCloud('add_list', data)
				// console.log(res);
				this.$operate.toast({
					title: res.result.msg
				})
				if (res.result.msg == '添加成功') {
					this.contacts = null; //	（联系人）
					this.phone = null; //（电话）
					this.place = null; //（地址）
					this.textarea = null; //（问题描述）
					this.imgList = [];
				}
			},
			async uploadingImg() {
				let arrar = this.imgList
				let that = this
				for (var i = 0; i < arrar.length; i++) {
					//图片上传
					const res = await uniCloud.uploadFile({
						filePath: arrar[i],
						cloudPath: arrar[i] + '.jpg',
						success(res) {
							console.log(res);
							that.imgDataList.push(res.fileID)
						}
					})
				}
			},
			// 选择上传图片或视频
			selectIm(e) {
				this.switchA = e.detail.value
			},
			// 选择视频
			ChooseVideo() {
				let that = this
				uni.chooseVideo({
					count: 1,
					maxDuration: 60,
					sourceType: ['album'],
					success: function(res) {
						console.log(res);
						that.video = res.tempFilePath;
					}
				});
			},
			//删除视频
			DelVideo() {
				uni.showModal({
					title: "提示",
					content: "是否要删除此视频",
					success: (res) => {
						if (res.confirm) {
							this.video = ''
						}
					}
				})
			},
			// 选择图片
			ChooseImage() {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['compressed'], //压缩图
					sourceType: ['camera', 'album'],
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			// 浏览图片
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			//删除图片
			DelImg(e) {
				uni.showModal({
					title: "提示",
					content: "是否要删除这张图片",
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			//打开地图选择位置。
			openMap() {
				let that = this
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						let longitude = res.longitude
						let latitude = res.latitude
						uni.chooseLocation({
							latitude: latitude,
							longitude: longitude,
							success: function(res) {
								that.place = res.address
								// console.log('位置名称：' + res.name);
								// console.log('详细地址：' + res.address);
								// console.log('纬度：' + res.latitude);
								// console.log('经度：' + res.longitude);
							}
						});
					}
				});
			},
			// 详情
			textareaAInput(e) {
				this.textareaAValue = e.detail.value
			},
			skipQuery() {
				this.$operate.redirect('/pages/query/query')
			}
		}
	}
</script>

<style>
	.video {
		width: 100%;
		height: 100%;
	}
</style>
